package com.bootdo.app.common.msg;

import lombok.Data;

import java.util.List;

/**
  * @auther: Along
  * @description: 数据响应类
  * @Date: created in 2020/12/8 21:46
  */
@Data
public class Response<T> {

    public static final String SUCCESS = "success";
    public static final String FAILED = "failed";
    public static final Integer SUCCESS_CODE = 200;
    public static final Integer FAILED_CODE = 500;

    private int code;// 响应码
    private String msg;// 响应消息
    private T data;// 响应数据
    private PageData<T> pageData;// 响应分页数据
    private Boolean success;

    public Response(){}
    public Response(int code,Boolean success,String msg){
        this.code = code;
        this.success = success;
        this.msg = msg;
    }

    public static Response response(int code,Boolean success,String msg){
        return response(code,success,msg,null);
    }
    public static <E>Response<E> response(int code,Boolean success,String msg,E data){
        Response<E> response = new Response<>();
        response.setCode(code);
        response.setSuccess(success);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    /**
     * 成功响应
     */
    public static Response success(){
        return success(SUCCESS);
    }
    public static Response success(String msg){
        return success(SUCCESS_CODE,msg);
    }
    public static Response success(int code,String msg){
        return success(code,true,msg,null);
    }
    public static <E>Response<E> success(E data){
        return success(SUCCESS_CODE,true,SUCCESS,data);
    }
    public static <E>Response<E> success(String msg,E data){
        return success(SUCCESS_CODE,true,msg,data);
    }
    public static <E>Response<E> success(int code,Boolean success,String msg,E data){
        return response(code,success,msg,data);
    }

    /**
     * 失败响应
     */
    public static Response fail(){
        return fail(FAILED);
    }
    public static Response fail(String msg){
        return fail(msg,null);
    }
    public static <E>Response<E> fail(E data){
        return fail(null,data);
    }
    public static <E>Response<E> fail(String msg,E data){
        return response(FAILED_CODE,false,msg,data);
    }

    /**
     * 分页响应
     */
    public static <E>Response<E> page(long total,List<E> rows){
        return page(SUCCESS,total,rows);
    }

    public static <E>Response<E> page(String msg,long total,List<E> rows){
        Response response = new Response();
        response.setCode(SUCCESS_CODE);
        response.setMsg(msg);
        response.setSuccess(true);
        response.setPageData(new PageData(total,rows));
        return response;
    }

}
